﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HCQ2_Model;

namespace HCQ2_BLL
{
    /// <summary>
    ///  用户--角色业务实现层
    /// </summary>
    public partial class T_UserRoleRelationBLL:HCQ2_IBLL.IT_UserRoleRelationBLL
    {
        /// <summary>
        ///  保存数据方法实现
        /// </summary>
        /// <param name="userRoles"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public bool SaveUserRoleData(string userRoles, int user_id)
        {
            if (user_id <= 0)
                return false;
            if (string.IsNullOrEmpty(userRoles))
            {
                Delete(s => s.user_id == user_id);
                return true;
            }
            //已配置的角色
            List<T_UserRoleRelation> list = DBSession.IT_UserRoleRelationDAL.GetUserRoleList(user_id.ToString());
            string[] str = userRoles.Trim(',').Split(',');
            StringBuilder sb = new StringBuilder();
            foreach (string role_id in str)
            {
                var roleCount = list.Where(s => s.role_id == HCQ2_Common.Helper.ToInt(role_id))?.Count();
                if (roleCount == null || roleCount <= 0)
                    sb.Append($"INSERT INTO T_UserRoleRelation(user_id,role_id) VALUES({user_id},{role_id});");
            }
            if(!string.IsNullOrEmpty(sb.ToString()))
                HCQ2_Common.SQL.SqlHelper.ExecuteNonQuery(sb.ToString());
            //删除已取消的角色
            HCQ2_Common.SQL.SqlHelper.ExecuteNonQuery($"DELETE FROM dbo.T_UserRoleRelation WHERE user_id={user_id} AND role_id NOT IN({userRoles.Trim(',')});");
            return true;
            //if (user_id <= 0)
            //    return false;
            ////保存之前删除之前设置的用户
            //Delete(s => s.user_id == user_id);
            //if (string.IsNullOrEmpty(userRoles))
            //    return true;
            //string[] str = userRoles.Trim(',').Split(',');
            //foreach (string item in str)
            //{
            //    Add(new T_UserRoleRelation()
            //    {
            //        user_id = user_id,
            //        role_id = HCQ2_Common.Helper.ToInt(item)
            //    });
            //}
            //return true;
        }
        /// <summary>
        ///  获取数据方法实现
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public List<T_UserRoleRelation> GetUserRoleData(int user_id)
        {
            if (user_id <= 0)
                return null;
            return Select(s => s.user_id == user_id);
        }

        /// <summary>
        ///  获取用户分配角色集合
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<T_UserRoleRelation> GetUserRoleList(string userid)
        {
            return DBSession.IT_UserRoleRelationDAL.GetUserRoleList(userid);
        }
    }
}
